﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses

Public Class EditTourCost
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack = True Then
            Fill_ComboBox(cmbTour, "select id, Name from ptn_tours where isactive=1 order by vieworder, Name")
            additem(cmbTour, "--Select--", 0)
            If Not Request.QueryString("id") Is Nothing Then
                fillData(Request.QueryString("id"))
            End If
        End If
    End Sub


    Private Sub fillData(ByVal Id As Integer)
        Dim arr As Array
        'arr = RetriveData("SELECT [Id], [TourId], [Pax_1], [Pax_2], [Pax_3], [Pax_4], [Pax_5], [Pax_6], [Pax_7], [Pax_8], [Pax_9], [Pax_10], [Pax_11], [Pax_12], [Pax_13], [Pax_14], [Pax_15], [Pax_16], [Pax_Child], [Single_Supp], [Valid], [ClientType] FROM [TourCost] where Id=" & Id, 21)
        arr = RetriveData("SELECT [Id], [TourId], [Pax_1], [Pax_2], [Pax_3], [Pax_4], [Pax_5], [Pax_6], [Pax_7], [Pax_8], [Pax_9], [Pax_Child], [Single_Supp], [Valid], [ClientType],[StartDate],[EndDate],[IsActive] FROM [ptn_TourCost] where Id=" & Id, 17)

        cmbTour.SelectedValue = arr(1)
        'B2B Price
        txtPax1.Text = formatPrice2(arr(2))
        txtPax2.Text = formatPrice2(arr(3))
        txtPax3.Text = formatPrice2(arr(4))
        txtPax4.Text = formatPrice2(arr(5))
        txtPax5.Text = formatPrice2(arr(6))
        txtPax6.Text = formatPrice2(arr(7))
        txtPax7.Text = formatPrice2(arr(8))
        txtPax8.Text = formatPrice2(arr(9))
        txtPax9.Text = formatPrice2(arr(10))
        'txtPax10.Text = formatPrice2(arr(11))
        'txtPax11.Text = formatPrice2(arr(12))
        'txtPax12.Text = formatPrice2(arr(13))
        'txtPax13.Text = formatPrice2(arr(14))
        'txtPax14.Text = formatPrice2(arr(15))
        'txtPax15.Text = formatPrice2(arr(16))
        'txtPax16.Text = formatPrice2(arr(17))
        txtChild.Text = formatPrice2(arr(11))
        txtSingle.Text = formatPrice2(arr(12))

        'txtValid.Text = arr(13)
        cmbClient.SelectedValue = arr(14)

        txtStartDate.Text = DisplayDate(arr(15))
        txtEndDate.Text = DisplayDate(arr(16))
        chkActive.Checked = arr(17)
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Dim sDate As DateTime
            Dim eDate As DateTime
            Dim strActive As String
            If chkActive.Checked = True Then
                strActive = 1
            Else
                strActive = 0
            End If
            'sDate = DateTime.ParseExact(txtStartDate.Text, "dd/MM/yyyy", Nothing)
            'eDate = DateTime.ParseExact(txtEndDate.Text, "dd/MM/yyyy", Nothing)
            If Request.QueryString("id") Is Nothing Then
                'sDate = DateTime.ParseExact(txtStartDate.Text, "dd/MM/yyyy", Nothing)
                'eDate = DateTime.ParseExact(txtEndDate.Text, "dd/MM/yyyy", Nothing)
                sDate = txtStartDate.Text
                eDate = txtEndDate.Text

                Dim txt As Array
                'txt = RetriveData("select Id from ptn_tourcost where startdate<='" & Date.Now & "' and enddate>='" & Date.Now & "' and tourid=" & cmbTour.SelectedValue, 0)
                txt = RetriveData("select * from ptn_tourcost where '" & eDate & "' between startdate and enddate or '" & sDate & "' between startdate and enddate and startdate<='" & sDate & "' and enddate<='" & eDate & "' and tourid=" & cmbTour.SelectedValue, 0)
                If txt(0) Is Nothing Then
                    Insert_Update_Delete_Record("INSERT INTO [ptn_TourCost]([TourId], [Pax_1], [Pax_2], [Pax_3], [Pax_4], [Pax_5], [Pax_6], [Pax_7], [Pax_8], [Pax_9], [Pax_Child], [Single_Supp], [ClientType],[StartDate],[EndDate],[Valid],[IsActive],[UpdatedDate]) VALUES(" & cmbTour.SelectedValue & ",'" & Server.HtmlEncode(txtPax1.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtPax2.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtPax3.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtPax4.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtPax5.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtPax6.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtPax7.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtPax8.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtPax9.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtChild.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtSingle.Text.Replace("'", "&#039;")) & "','" & cmbClient.SelectedValue & "','" & sDate & "','" & eDate & "',''," & strActive & ",'" & Date.Now & "')")
                    Response.Redirect("ViewTourCost.aspx")
                Else
                    lblMsg.Text = "This tous already has amount."
                End If
            Else
                Dim dateInfo As System.Globalization.DateTimeFormatInfo = New System.Globalization.DateTimeFormatInfo

                dateInfo.ShortDatePattern = "dd/MM/yyyy"
                Dim StartDate As Date = Convert.ToDateTime(txtStartDate.Text, dateInfo)
                Dim EndDate As Date = Convert.ToDateTime(txtEndDate.Text, dateInfo)
                Insert_Update_Delete_Record("UPDATE [ptn_TourCost] SET [TourId]=" & cmbTour.SelectedValue & ",[Pax_1]='" & Server.HtmlEncode(txtPax1.Text.Replace("'", "&#039;")) & "',[Pax_2]='" & Server.HtmlEncode(txtPax2.Text.Replace("'", "&#039;")) & "',[Pax_3]='" & Server.HtmlEncode(txtPax3.Text.Replace("'", "&#039;")) & "',[Pax_4]='" & Server.HtmlEncode(txtPax4.Text.Replace("'", "&#039;")) & "',[Pax_5]='" & Server.HtmlEncode(txtPax5.Text.Replace("'", "&#039;")) & "',[Pax_6]='" & Server.HtmlEncode(txtPax6.Text.Replace("'", "&#039;")) & "',[Pax_7]='" & Server.HtmlEncode(txtPax7.Text.Replace("'", "&#039;")) & "',[Pax_8]='" & Server.HtmlEncode(txtPax8.Text.Replace("'", "&#039;")) & "',[Pax_9]='" & Server.HtmlEncode(txtPax9.Text.Replace("'", "&#039;")) & "',[Pax_Child]='" & Server.HtmlEncode(txtChild.Text.Replace("'", "&#039;")) & "',[Single_Supp]='" & Server.HtmlEncode(txtSingle.Text.Replace("'", "&#039;")) & "',[ClientType]='" & cmbClient.SelectedValue & "',[StartDate]='" & StartDate & "',[EndDate]='" & EndDate & "',[UpdatedDate]='" & Date.Now & "' where id=" & Request.QueryString("Id"))
                Response.Redirect("ViewTourCost.aspx")

            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("ViewTourCost.aspx")
    End Sub

End Class